package com.study.common;

public class Test {
    public static void main(String[] args) {
        Product product = new Product(0);

        new Thread(() -> {

            for (int i = 0; i < 10; i++) {
                product.product();
            }

        }, "生产者").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                product.consume();
            }
        }, "消费者").start();

//        new Thread(() -> {
//
//            for (int i = 0; i < 10; i++) {
//                product.product();
//            }
//
//        }, "生产者2").start();
//
//        new Thread(() -> {
//            for (int i = 0; i < 10; i++) {
//                product.consume();
//            }
//        }, "消费者2").start();

    }

}

class Product {
    public   int num;

    public Product(int num) {
        this.num = num;
    }

    synchronized void product() {
        if (num < 10) {
            num++;
            System.out.println(Thread.currentThread().getName() + "生产了==============" + num);
        }
        this.notify();
    }

    synchronized void consume() {
        if (num > 0) {
            System.out.println(Thread.currentThread().getName() + "消费了**************" + num);
            num =num-1;
//            this.notify();
        }else {
            try {
                System.out.println("消费者等待");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}